package com.whfc.common.third.map.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.whfc.common.third.map.MapApi;
import com.whfc.common.third.map.MapLoc;
import com.whfc.common.util.Gps;
import com.whfc.common.util.HttpUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @Description: 高德地图API
 * @author: xugcheng
 * @version: 1.0
 * @date: 2021-10-13 10:48
 */
public class AMapApi implements MapApi {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    /**
     * 地址位置编码接口URL
     */
    public static final String GEOURL = "http://restapi.amap.com/v3/geocode/geo";

    /**
     * 地理位置逆编码接口URL
     */
    public static final String REGEO_URL = "http://restapi.amap.com/v3/geocode/regeo";

    /**
     * 坐标转换URL
     */
    public static final String CONVERT_URL = "https://restapi.amap.com/v3/assistant/coordinate/convert";

    /**
     * 接口默认返回JSON格式
     */
    public static final String DEFAULT_OUTPUT = "json";

    /**
     * 接口调用成功状态码
     */
    public static final int STATUS_SUCCESS = 1;

    public static final int INFOCODE_SUCCESS = 10000;

    public AMapApi() {
    }

    public AMapApi(String key) {
        this.key = key;
    }

    private String key;

    public String getKey() {
        return key;
    }

    public void setKey(String key) {
        this.key = key;
    }

    @Override
    public Gps translateToGcj02(Double lngWgs84, Double latWgs84) {
        //https://restapi.amap.com/v3/assistant/coordinate/convert?locations=116.481499,39.990475&coordsys=gps&output=xml&key=<用户的key>
        String location = new StringBuilder()
                .append(lngWgs84)
                .append(",")
                .append(latWgs84)
                .toString();
        String url = new StringBuilder(CONVERT_URL)
                .append("?key=").append(key)
                .append("&locations=").append(location)
                .append("&coordsys=gps")
                .append("&output=json")
                .toString();
        String response = HttpUtil.doGet(url);
        logger.info("response:{}", response);
        if (response != null && response.length() > 0) {
            JSONObject resJSON = JSON.parseObject(response);
            if (resJSON != null && resJSON.containsKey("status") && resJSON.containsKey("infocode")) {
                if (STATUS_SUCCESS == resJSON.getIntValue("status") && INFOCODE_SUCCESS == resJSON.getIntValue("infocode")) {
                    String retLocation = resJSON.getString("locations");
                    String[] strArr = retLocation.split(",");
                    Double lng = Double.valueOf(strArr[0]);
                    Double lat = Double.valueOf(strArr[1]);
                    return new Gps(lat, lng);
                }
            }
        }
        return new Gps(latWgs84, lngWgs84);
    }

    @Override
    public MapLoc geocode(Double lng, Double lat) {
        //http://restapi.amap.com/v3/geocode/regeo?output=xml&location=116.310003,39.991957&key=<用户的key>&radius=1000&extensions=all

        String location = new StringBuilder()
                .append(lng)
                .append(",")
                .append(lat)
                .toString();

        String url = new StringBuilder(REGEO_URL)
                .append("?key=").append(key)
                .append("&location=").append(location)
                .append("&output=").append(DEFAULT_OUTPUT)
                .toString();

        String response = HttpUtil.doGet(url);
        logger.info("response:{}", response);
        if (response != null && response.length() > 0) {
            JSONObject resJSON = JSON.parseObject(response);
            if (resJSON != null && resJSON.containsKey("status") && resJSON.containsKey("infocode")) {
                if (STATUS_SUCCESS == resJSON.getIntValue("status") && INFOCODE_SUCCESS == resJSON.getIntValue("infocode")) {
                    JSONObject addressComponent = resJSON.getJSONObject("regeocode").getJSONObject("addressComponent");
                    String province = addressComponent.getString("province");
                    String city = addressComponent.getString("city");
                    String district = addressComponent.getString("district");

                    MapLoc loc = new MapLoc();
                    loc.setProvince("[]".equals(province) ? null : province);
                    loc.setCity("[]".equals(city) ? null : city);
                    loc.setDistrict("[]".equals(district) ? null : district);
                    return loc;
                }
            }
        }
        return new MapLoc();
    }
}
